home *** CD-ROM | disk | FTP | other *** search
- Path: taco.cc.ncsu.edu!not-for-mail
- From: zaykin@espcbw.stat.ncsu.edu (Dmitri Zaykin)
- Newsgroups: comp.lang.c++
- Subject: Re: dynamic 3d array of user defined variables(need help)
- Date: 16 Mar 1996 04:19:40 GMT
- Organization: Statistical Eugenics
- Distribution: world
- Message-ID: <4idfgs$t6m@taco.cc.ncsu.edu>
- References: <4iak05$3mg@cafu.fl.net.au>
- NNTP-Posting-Host: espcbw.stat.ncsu.edu
- X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
-
- don@fl.net.au wrote:
- > I have been trying to write a function to create a dynamic 3d array
- > of user defined variables in Borland C++ 4.5 without much success.I
- > have managed to write a flexible function to define a 3d array but I
- > need to define one of co-ordinates and am having trouble with user
- > defined variables. I would appreciate any help you can offer.
-
- this should work:
-
- template <class T>
- T ***Trialloc(T *dummy, int fir, int sec, int thi)
- { // ignore 'param dummy never used' warning
- T ***d;
- int i,j;
- d = new T**[ fir ];
- for(i=0; i<fir; i++)
- {
- d[i] = new T*[ sec ];
- for(j=0; j<sec; j++) {
- d[i][j] = new T[ thi ];
- }
- }
- return d;
- }
-
- template<class T>
- void Trifree(T ***x, int fir, int sec)
- {
- int i,j;
- for(i=0; i < fir; i++) {
- for(j=0; j < sec; j++) { delete [] x[i][j]; }
- }
- for(i=0; i < fir; i++) { delete [] x[i]; }
- delete [] x;
- };
-
-
- // example
-
- struct Data {
- char buf[2];
- Data *next;
- Data() { buf[0]=buf[1]=0; next=0; }
- };
-
- int main(void)
- {
- Data *** x;
- Data tmp; // need 'tmp' to specify the data type when call 'Trialloc'
- x = Trialloc(&tmp, 10, 20, 30);
- // do something with x[i][j][k]
- Trifree(x, 10, 20);
- return 0;
- }
-
- --
- Dima
- { the following appendum is a Cauchy random variable }
- The opera ain't over till the fat lady sings
-